<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('pr'))
{
	function pr($item, $exit=false)
	{
		echo "<pre/>";
		if(is_array($item) || is_object($item)){
			var_dump($item);
		} else {
			echo $item;
		}
		if($exit){
			exit();
		}
	}
}

if ( ! function_exists('last_query'))
{
	function last_query($exit=false)
	{
		$CI =& get_instance();
		echo $CI ->db->last_query();
		if($exit){
			exit();
		}
	}
}

/****************************	BEGIN: INTERNET HELPER ***************************/
function getIP(){
    if (!empty($_SERVER['HTTP_CLIENT_IP']))  //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   
    //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
/****************************	END: INTERNET HELPER ***************************/

/**
 * getNow()
 *
 * @return
 */
if ( ! function_exists('getNow'))
{
	function getNow()
	{
		return date(DATETIME_FORMAT_DB);
	}
	
	function getToday($day_adding=0)
	{
		$today = date(DATETIME_FORMAT_DB_NO_TIME);  //Lay ngay hien tai
		$today = strtotime($today);   // chuyen chuyen thanh thoi gian
		$result = $today + $day_adding*3600*24; 
		$result = date(DATETIME_FORMAT_DB,$result);
		return $result;
	}
}

if ( ! function_exists('resize_image'))
{
	function resize_image($source_dir, $file_name, $width, $height, $des_dir)
	{
		require_once 'phpthumb/ThumbLib.inc.php';  
		$thumb = PhpThumbFactory::create($source_dir.$file_name);  
		$thumb->resize($width, $height);
		$thumb->save($des_dir.$file_name);
	}
}

function remove_vietnamese_accents($str)
{
	$accents_arr=array(
		"à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
		"ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề",
		"ế","ệ","ể","ễ",
		"ì","í","ị","ỉ","ĩ",
		"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ",
		"ờ","ớ","ợ","ở","ỡ",
		"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
		"ỳ","ý","ỵ","ỷ","ỹ",
		"đ",
		"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă",
		"Ằ","Ắ","Ặ","Ẳ","Ẵ",
		"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
		"Ì","Í","Ị","Ỉ","Ĩ",
		"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ",
		"Ờ","Ớ","Ợ","Ở","Ỡ",
		"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
		"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
		"Đ"," ","-","/","’","`","'",'"'
	);

	$no_accents_arr=array(
		"a","a","a","a","a","a","a","a","a","a","a",
		"a","a","a","a","a","a",
		"e","e","e","e","e","e","e","e","e","e","e",
		"i","i","i","i","i",
		"o","o","o","o","o","o","o","o","o","o","o","o",
		"o","o","o","o","o",
		"u","u","u","u","u","u","u","u","u","u","u",
		"y","y","y","y","y",
		"d",
		"A","A","A","A","A","A","A","A","A","A","A","A",
		"A","A","A","A","A",
		"E","E","E","E","E","E","E","E","E","E","E",
		"I","I","I","I","I",
		"O","O","O","O","O","O","O","O","O","O","O","O",
		"O","O","O","O","O",
		"U","U","U","U","U","U","U","U","U","U","U",
		"Y","Y","Y","Y","Y",
		"D","_","_","_","","","",""
	);

    $str = str_replace($accents_arr,$no_accents_arr,$str);
    //$str = strtr($str, array(
	//						' '=>'-',"'"=>'','"'=>'','�'=>'','�'=>'','�'=>'',
	//						':'=>'','.'=>'',','=>'','�'=>'(R)','�'=>'(C)', '?' => '', '#'=>'', '*'=>''));
    $str = cleanUrl($str);
	return $str;
}

function cleanUrl($str) {
	$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str);
	$clean = strtolower(trim($clean, '-'));
	$clean = preg_replace("/[\/_|+ -]+/", '-', $clean);
    $clean = strtolower(trim($clean, '-'));
    
	return $clean;
}

if ( ! function_exists('watermark'))
{
	function watermark($image_file_path, $watermark, $dest_x=0, $dest_y=0)
	{
		require_once 'phpthumb/ThumbLib.inc.php';
		$thumb = PhpThumbFactory::create($image_file_path);
		$thumb->createWatermark($watermark, $dest_x, $dest_y);
        
		$thumb->save($image_file_path);
	}
}

if ( ! function_exists('CutText')){
	function CutText($text, $n=80) 
	{ 
		// string is shorter than n, return as is
		if (strlen($text) <= $n) {
			return $text;}
		$text= substr($text, 0, $n);
		if ($text[$n-1] == ' ') {
			return trim($text)."...";
		}
		$x  = explode(" ", $text);
		$sz = sizeof($x);
		if ($sz <= 1)   {
			return $text."...";}
		$x[$sz-1] = '';
		return trim(implode(" ", $x))."...";
	}
}

function parserEditorHTML($str){
	$str = str_replace('../../../static/editor/', base_url().'static/editor/', $str);
	//$str = str_replace('http://demo.climaxinteractive.com/kohler/static/uploads/editor/', base_url().'static/uploads/editor/', $str);
	return $str;
}
?>